﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>地址管理</title>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/ydui.css?v=1.1.4" rel="stylesheet" />
    <script src="../js/ydui.flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/all.css?v=20180302" />
    <style type="text/css">
        .m-cell {
            background: none;
        }

            .m-cell:after {
                border: none;
            }

        .cell-item {
            margin-top: .2rem;
            background: #fff;
            border-top: 1px solid #E7E7E7;
            border-bottom: 1px solid #e7e7e7;
        }
    </style>
</head>
<body ms-controller="addressview" class="ms-controller">
    <div class="g-flexview">
        <header class="m-navbar">
            <a onclick="history.back()" class="navbar-item"><i class="back-ico"></i></a>
            <div class="navbar-center"><span class="navbar-title">地址管理</span></div>
        </header>
        <section class="g-scrollview">
            <div class="manage_box" ms-for="el in @ListData">
                <div class="manage_list" ms-on-tap="@Choose(el)">
                    <div class="manage_text">
                        <p class="iconfont ico icon-dingwei fl"></p>
                        <div class="textera fr">
                            <p>{{el.Receiver}}<span class="fr">{{el.Telephone}}</span></p>
                            <p style="margin-top: 5px;"><span style="padding-right:5px;">{{el.Title}}</span>{{el.Address}}</p>
                        </div>
                    </div>
                </div>
                <div class="manage_btn">
                    <label>
                        <input type="radio" name="address" ms-attr="{value:el.IsDefault}" ms-on-tap="@SetDefault($event,el)" >
                        <div class="default_text">
                            <span class="iconfont icon-duihao1"></span>
                            <p class="fl">默认地址</p>
                        </div>
                    </label>
                    <div class="btn_edit">
                        <a ms-attr="{href:'addressadd.html?id='+el.ID}"><span class="iconfont icon-bianji"></span>编辑</a>
                        <a ms-on-tap="@Del(el)"><span class="iconfont icon-delete1"></span>删除</a>
                    </div>
                </div>
            </div>
        </section>
        <a href="addressadd.html" class="btn  btn-primary" style="width:95%;margin:0.2rem;">
            添加新地址
        </a>

    </div>

    <script src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="../js/ydui.js"></script>
    <script src="../js/avalon.mobile.min.js"></script>
    <script src="../js/ComFunJS.js?v=20180331"></script>
    <script type="text/javascript">

        var model = avalon.define({
            $id: "addressview",
            ListData: [],
            returnurl: ComFunJS.getQueryString("returnurl"),
            get: function () {
                $.getJSON("/api/user/address/list", function (r) {
                    model.ListData = r.result;
                    $(".manage_btn").find("input[type=radio]").each(function (index, value) {
                        if ($(value).attr("value") == "1") {
                            $(value).attr("checked", "checked");
                        }
                    })
                })
            },
            Del: function (el) {
                YDUI.dialog.confirm("提示", "确定要删除吗?", function () {
                    $.getJSON("/api/user/address/del/" + el.ID, function (r) {
                        if (r.error == "") {
                            model.ListData.remove(el);
                        }
                    })

                })

            },
            SetDefault: function (e, el) {
                if (el.IsDefault != 1) {
                    model.ListData.forEach(function (value, index) {
                        value.IsDefault = 0;
                    })
                    el.IsDefault = 1;
                    $.getJSON("/api/user/address/setdefault/" + el.ID, function (r) { });
                }
            },
            Choose: function (el) {
                $.getJSON("/api/user/address/setdefault/" + el.ID, function (r) {
                    if (model.returnurl) {
                        window.location = model.returnurl;
                    } else {
                        window.location = "../order/new.html";
                    }
                    
                });
            }
        })
        avalon.ready(function () {
            if (ComFunJS.isLogin(true)) {
                model.get();
            }

        })

    </script>
</body>
</html>
